
from rest_framework import serializers
from orders.models import OrderInfo, OrderGoods
from goods.models import SKU

class OrderSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'create_time'
        ]


#单独定义一个用于订单详情的序列化器
class SKUSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = [
            'name',
            'default_image'
        ]


#当前订单商品对象关联的单一的sku对象
class OrderGoodsModelSerializer(serializers.ModelSerializer):
    sku = SKUSimpleSerializer()

    class Meta:
        model = OrderGoods
        fields = [
            'count',
            'price',
            'sku',  #关联主表ｓｋｕ单一数据
        ]

#当前订单对象关联的多个订单商品OrderGoods对象
class OrderDetailSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField()

    skus = OrderGoodsModelSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = [
            #固有字段
            'order_id',
            'total_count',
            'total_amount',
            'freight',
            'pay_method',
            'status',
            'create_time',

            #主表关联字段（显示定义）
            'user',

            #从表关联字段（隐藏定义）
            #关联的从表－－订单商品（OrderGoods）中间表多条数据
            'skus',
        ]